package com.example.myapp;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapp.entity.CommentInfo;

import java.util.ArrayList;
import java.util.List;

public class CommentListAdapter extends RecyclerView.Adapter<CommentListAdapter.MyHolder> {

    private List<CommentInfo> mCommentInfoList = new ArrayList<>();


    public void setCommentInfoList(List<CommentInfo> list) {
        this.mCommentInfoList = list;
        //这句话不能少
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_list_item, parent, false);

        return new MyHolder(view);
    }

    @Override
    @SuppressLint("RecyclerView")
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {

        //绑定数据
        CommentInfo commentInfo = mCommentInfoList.get(position);
        holder.tv_username.setText(commentInfo.getUsername());
        holder.tv_content.setText(commentInfo.getComment_content());
    }

    @Override
    public int getItemCount() {
        return mCommentInfoList.size();
    }

    static class MyHolder extends RecyclerView.ViewHolder {

        TextView tv_content;
        TextView tv_username;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            tv_content = itemView.findViewById(R.id.tv_content);
            tv_username = itemView.findViewById(R.id.tv_username);

        }
    }

}

